#include "func.h"
int recvn(int sockfd, void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        ssize_t sret = recv(sockfd, p+total, length-total, 0);
        total += sret;
    }
    return total;
}



int recvFile(int sockfd,char* bufc){
/////////////////////////////////////////////
    char bufcname[1024];
    strcpy(bufcname,bufc);
    int judgefd =open("filename",O_RDWR);
    if(!judgefd){
    train_t train;
    memset(&train, 0, sizeof(train));
    struct stat statbuf;
    train.length = sizeof(int);
    fstat(judgefd,&statbuf);
    char bufi[1024];
    sprintf(bufi,"%ld",statbuf.st_size);
    strcpy(train.data,bufi);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    }else{
    train_t train;
    memset(&train, 0, sizeof(train));
    struct stat statbuf;
    stat(bufcname,&statbuf);
    char bufi[1024];
    sprintf(bufi,"%d",(int)statbuf.st_size);

    //test
    printf("获取的已有文件字节数为statbuf.st_size：%ld \n",statbuf.st_size);

    printf("由int类型转化成的字符串是bufi：%s\n",bufi);

    train.length = strlen(bufi);
    strcpy(train.data,bufi);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    }
//////////////////////////////////////////////
    char filename[1024] = {0};
    int length;
    recvn(sockfd, &length, sizeof(length));
    recvn(sockfd, filename, length);
    off_t filesize;
    recvn(sockfd, &length, sizeof(length));
    recvn(sockfd, &filesize, length);
    printf("filesize = %ld\n", filesize);
    int fd = open(filename, O_RDWR|O_APPEND, 0666);
    off_t cursize = 0;
    off_t slice = filesize/10000;
    off_t lastsize = 0;
    while(1){
        char buf[4096] = {0};
        recvn(sockfd, &length, sizeof(length));
        if(length != 1000){
            printf("length = %d\n", length);
        }
        if(length == 0){
            break;
        }
        recvn(sockfd, buf, length);
        write(fd, buf, length);

        cursize += length;
        if(cursize - lastsize > slice){
            printf("%5.2lf%%\r",100.0*cursize/filesize);
            fflush(stdout);//将stdout缓冲区的数据刷到文件对象里面
            lastsize = cursize;
        }
    }
    printf("100.00%%\n");
    close(fd);
    return 0;
}

int putsFile(int sockfd, const char *loginname,const char *filename)
{
    train_t train;
    memset(&train, 0, sizeof(train));
    //发送用户名和文件名
    train.length = strlen(loginname);
    strcpy(train.data, loginname);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    //只发送文件名 
    train.length = strlen(filename);
    strcpy(train.data, filename);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    int fd = open(filename, O_RDWR);

    struct stat statbuf;
    fstat(fd,&statbuf);

    train.length = sizeof(statbuf.st_size);
    memcpy(train.data,&statbuf.st_size,train.length);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    ERROR_CHECK(p,MAP_FAILED,"mmap");

    off_t totalsize = 0;

    while(totalsize < statbuf.st_size)
    {
        if(statbuf.st_size>1000)
        {
            train.length = 1000;
        }
        else
        {
            train.length = statbuf.st_size-totalsize;
        }
        send(sockfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
        send(sockfd,p+totalsize,train.length,MSG_NOSIGNAL);
        totalsize += train.length;
    }
    train.length = 0;
    send(sockfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    munmap(p,statbuf.st_size);
    close(fd);
    return 0;
}

